<?
/** CONNEXION BANQUE CMCIC
  Kob-Eye 2009
                       **/

//GLOBALES
//@todo Gestion des échéances

define("CMCIC_CTLHMAC","V1.04.sha1.php--[CtlHmac%s%s]-%s");
define("CMCIC_CTLHMACSTR", "CtlHmac%s%s");
define("CMCIC_CGI2_RECEIPT","version=2\ncdr=%s");
define("CMCIC_CGI2_MACOK","0");
define("CMCIC_CGI2_MACNOTOK","1\n");
//define("CMCIC_CGI2_FIELDS", "%s*%s*%s*%s*%s*%s*%s*%s*%s*%s*%s*%s*%s*%s*%s*%s*%s*%s*%s*%s*");
//define("CMCIC_CGI1_FIELDS", "%s*%s*%s*%s*%s*%s*%s*%s*%s*%s*%s*%s*%s*%s*%s*%s*%sha1*%s*%s*%s");
define("CMCIC_CGI2_FIELDS", "%s*%s*%s*%s*%s*%s*%s*%s*%s*%s*%s*%s*%s*%s*%s*%s*%s*%s*%s*%s*");
define("CMCIC_CGI1_FIELDS", "%s*%s*%s%s*%s*%s*%s*%s*%s*%s*%s*%s*%s*%s*%s*%s*%s*%s*%s*%s");
define("CMCIC_URLPAIEMENT", "paiement.cgi");
$pre = "MODULE::BOUTIQUE::PAIEMENT::CMCIC::";
define("CMCIC_CLE",$GLOBALS["Systeme"]->Conf->get($pre . "CLE") );
define("CMCIC_TPE",$GLOBALS["Systeme"]->Conf->get($pre . "TPE") ); 
define("CMCIC_VERSION",$GLOBALS["Systeme"]->Conf->get($pre . "VERSION") );
define("CMCIC_SERVEUR",$GLOBALS["Systeme"]->Conf->get($pre . "SERVEUR") );
define("CMCIC_CODESOCIETE",$GLOBALS["Systeme"]->Conf->get($pre . "CODESOCIETE") );
define("CMCIC_URLOK",$GLOBALS["Systeme"]->Conf->get($pre . "URLOK") );
define("CMCIC_URLKO",$GLOBALS["Systeme"]->Conf->get($pre . "URLKO") );

require_once("Class/Lib/CMCIC_Tpe.inc.php");

class CmCic
{

//    var $langue = "Francais";
    var $langue = "FR";                 // ??    
    var $options= "";                        // ??
    var $ref;                                // Générée à la construction
    var $montant = -1.0;
    var $devise = "EUR";
    var $texteLibre = "TLibre";         // ??
    var $dateTransaction;
    var $customerMail;
    var $codeSociete = "543";
    
    var $urls=array("http://h2o.kob-eye.com","http://h2o.kob-eye.com","http://h2o.kob-eye.com");               // Respectivement : url d'enregistrement, url ok, url ko
    //Champs calculés
    var $oTpe;
    var $oHmac;
    var $mac;
    
   /**
     * Constructeur de la classe CmCic
     * 
     */function __construct() {
         $this->ref = "P".(string)mktime() . (string)(rand()%9);
         $this->dateTransaction = date("d/m/Y:H:i:s");
     }


   /**
     * Affecte le bon montant à la transaction
     *
     * @param float $montant Montant de la transaction 
     */function setMontant( $montant ) {
         $this->montant = (float)$montant;
     }



   /**
     * Affecte le mail client à la transaction
     *
     * @param String $email Mail du client 
     */function setCustomerMail( $email ) {
         $this->customerMail = (string)$email;
     }


   /**
     * Construction des champs calculés
     * 
     */function build() {
         $this->oTpe = new CMCIC_Tpe($this->langue);
         $this->oHmac = new CMCIC_Hmac($this->oTpe);
         $CtlHmac = sprintf(CMCIC_CTLHMAC,
                            $this->oTpe->sVersion,
                            $this->oTpe->sNumero,
                            $this->oHmac->computeHmac(sprintf(CMCIC_CTLHMACSTR, $this->oTpe->sVersion, $this->oTpe->sNumero)));
         //On vide les champs relatifs aux échéances;
         $sNbrEch = $sDateEcheance1 = $sMontantEcheance1 = "";
         $sDateEcheance2 = $sMontantEcheance2 = "";
         $sDateEcheance3 = $sMontantEcheance3 = "";
         $sDateEcheance4 = $sMontantEcheance4 = ""; 
         $PHP1_FIELDS = sprintf(CMCIC_CGI1_FIELDS,
                                $this->oTpe->sNumero,
                                $this->dateTransaction,
                                $this->montant,$this->devise,
                                $this->ref,
                                $this->texteLibre,
                                $this->oTpe->sVersion,
                                $this->oTpe->sLangue,
                                $this->oTpe->sCodeSociete, 
                                $this->customerMail,
                                $sNbrEch,
                                $sDateEcheance1,
                                $sMontantEcheance1,
                                $sDateEcheance2,
                                $sMontantEcheance2,
                                $sDateEcheance3,
                                $sMontantEcheance3,
                                $sDateEcheance4,
                                $sMontantEcheance4,"");
         echo($PHP1_FIELDS);
         $this->mac = $this->oHmac->computeHmac($PHP1_FIELDS);
     }


   /**
     * Génère un tableau nom/valeur de champs cachés pour le formulaire
     *
     * @return String
     */function getHiddenInputs() {
         echo($this->mac);
         
         return array(array("name" => "version", "value" => $this->oTpe->sVersion)
                     ,array('name'=>'TPE','value'=>$this->oTpe->sNumero)
                     ,array('name'=>'date','value'=>$this->dateTransaction)
                     ,array('name'=>'montant','value'=>$this->montant.$this->devise)
                     ,array('name'=>'reference','value'=>$this->ref)
                     ,array('name'=>'MAC','value'=>$this->mac)
                     ,array('name'=>'url_retour','value'=>$this->urls[0])
                     ,array('name'=>'url_retour_ok','value'=>$this->urls[1])
                     ,array('name'=>'url_retour_err','value'=>$this->urls[2])
                     ,array('name'=>'lgue','value'=>$this->langue)
                     ,array('name'=>'societe','value'=>$this->oTpe->sCodeSociete)
                     ,array('name'=>'texte-libre','value'=>$this->texteLibre)
                     ,array('name'=>'mail','value'=>$this->customerMail)
//                     ,array('name'=>'options','value'=>$this->options)
                     );
     }


   /**
     * Renvoie l'url pour l'action du formulaire
     *
     * @return String
     */function getUrlPaiement() {
         return $this->oTpe->sUrlPaiement;
     }
}